package com.yxw.springmvc.userBehavior.aop;

import com.alibaba.fastjson.JSONObject;
import com.yxw.springmvc.userBehavior.helper.InterfaceType;
import com.yxw.springmvc.userBehavior.model.User;
import com.yxw.springmvc.userBehavior.mq.ServiceMQSendManage;
import com.yxw.springmvc.userBehavior.redis.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by hp on 2016/9/18.
 */
@Component
@Aspect
public class MvcServiceAopManage {

    @Resource
    private ServiceMQSendManage serviceMQSendManage;

    @Resource
    private RedisUtil redisUtil;

    /**
     * @param joinPoint
     * @param rObj
     */
    @AfterReturning(pointcut = "com.yxw.springmvc.userBehavior.aop.ServicePointCutManage.service()",returning = "rObj")
    public void sendMsg(JoinPoint joinPoint, Object rObj) {
            Object[] args = joinPoint.getArgs(); // 获得参数数组
            Object request = args[0]; // 获得参数
            User user = (User) request; // 参数类型转换
            Object cObj = joinPoint.getThis(); // 获得当前接口对象
            String methodName = joinPoint.getSignature().getName(); // 获得接口方法名

            if("insert".equals(methodName)){
                //给mq发信息
                serviceMQSendManage.sendMsg(InterfaceType.USER_REGISTER,user);
                //给redis保存新增用户的信息
                redisUtil.set(user.getUserCode(), JSONObject.toJSONString(user));
            }

    }
}
